<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>class Enumerator - RDoc Documentation</title>


<script src="./js/navigation.js" defer></script>
<script src="./js/search.js" defer></script>
<script src="./js/search_index.js" defer></script>
<script src="./js/searcher.js" defer></script>
<script src="./js/darkfish.js" defer></script>

<script src="./js/jquery-3.2.0.min.js"></script>

<script src="./js/vue.min.js"></script>
<script src="./js/js.cookie.min.js"></script>

<link href="./css/fonts.css" rel="stylesheet">
<link id='rdoccss' href="./css/rdoc.css" rel="stylesheet">
<link href="./css/carbon17.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "./";
  var index_rel_prefix = "./";
  var darkModeCsseHref = "./css/rdoc-dm.css"
  var defaultModeCssHref = "./css/rdoc.css"
  // var cssDarkmode = Cookies.get('darkmode');
  
  if( Cookies.get("darkmode") == "true") {
	$('#rdoccss').attr("href", darkModeCsseHref);
}

//  https://cssdeck.com/blog/simple-jquery-stylesheet-switcher/

document.write('<style type="text/css">body{display:none}</style>');

</script>


</head>
<body id="top" role="document" class="class">
  <!-- this is class.html -->

  <div id='actionbar' >
    <div class='wrapper mdiv'>
      <ul class='grids g0'></ul>
    </div> 
    <!-- VERSION HEADER for 3.3.0-preview2 NOT FOUND -->
  </div> <!-- end action bar -->

  <div class='wrapper hdiv'>

    


    <nav id='vapp' role="navigation">
    <div id="project-navigation">
      <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2><a href="./index.html" rel="home">Home</a></h2>

  <div id="table-of-contents-navigation"  >
    <a href="./table_of_contents.html#pages">Pages</a>
    <a href="./table_of_contents.html#classes">Classes</a>
    <a href="./table_of_contents.html#methods">Methods</a>
  </div>
</div>

      <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

    </div>


    
<div class="nav-section">
  <h3>Table of Contents</h3>

  <ul class="link-list" role="directory">
    <li><a href="#class-Enumerator-label-External+Iteration">External Iteration</a>
    <li><a href="#class-Enumerator-label-Convert+External+Iteration+to+Internal+Iteration">Convert External Iteration to Internal Iteration</a>
  </ul>
</div>


    <button id='toggleThing' @click="toggleNav()" >Show/hide navigation</button>
    <div :class="isOpen ? 'block' : 'hidden' " id='toggleMe'>
      <div id="class-metadata">
        
        
<div id="parent-class-section" class="nav-section">
  <h3>Parent</h3>

  <p class="link"><a href="Object.html">Object</a>
</div>

        
<div id="includes-section" class="nav-section">
  <h3>Included Modules</h3>

  <ul class="link-list">
    <li><a class="include" href="Enumerable.html">Enumerable</a>
  </ul>
</div>

        
        
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
  <h3>Methods</h3>

  <ul class="link-list" role="directory">
    <li ><a href="#method-c-new">::new</a>
    <li ><a href="#method-c-produce">::produce</a>
    <li ><a href="#method-c-product">::product</a>
    <li ><a href="#method-i-2B">#+</a>
    <li ><a href="#method-i-each">#each</a>
    <li ><a href="#method-i-each_with_index">#each_with_index</a>
    <li ><a href="#method-i-each_with_object">#each_with_object</a>
    <li ><a href="#method-i-feed">#feed</a>
    <li ><a href="#method-i-inspect">#inspect</a>
    <li ><a href="#method-i-next">#next</a>
    <li ><a href="#method-i-next_values">#next_values</a>
    <li ><a href="#method-i-peek">#peek</a>
    <li ><a href="#method-i-peek_values">#peek_values</a>
    <li ><a href="#method-i-rewind">#rewind</a>
    <li ><a href="#method-i-size">#size</a>
    <li ><a href="#method-i-with_index">#with_index</a>
    <li ><a href="#method-i-with_object">#with_object</a>
  </ul>
</div>

      </div>
     </div>
    </nav>


    <div id='extraz'><div class='adzbox-index'  >
      
     </div>         
    </div>

    <main role="main" aria-labelledby="class-Enumerator">
    <h1 id="class-Enumerator" class="class">
      class Enumerator
    </h1>

    <section class="description">
    
<p>A class which allows both internal and external iteration.</p>

<p>An <a href="Enumerator.html"><code>Enumerator</code></a> can be created by the following methods.</p>
<ul><li>
<p><a href="Object.html#method-i-to_enum"><code>Object#to_enum</code></a></p>
</li><li>
<p><a href="Object.html#method-i-enum_for"><code>Object#enum_for</code></a></p>
</li><li>
<p><a href="Enumerator.html#method-c-new"><code>Enumerator.new</code></a></p>
</li></ul>

<p>Most methods have two forms: a block form where the contents are evaluated for each item in the enumeration, and a non-block form which returns a new <a href="Enumerator.html"><code>Enumerator</code></a> wrapping the iteration.</p>

<pre class="ruby"><span class="ruby-identifier">enumerator</span> = <span class="ruby-node">%w(one two three)</span>.<span class="ruby-identifier">each</span>
<span class="ruby-identifier">puts</span> <span class="ruby-identifier">enumerator</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># =&gt; Enumerator</span>

<span class="ruby-identifier">enumerator</span>.<span class="ruby-identifier">each_with_object</span>(<span class="ruby-string">&quot;foo&quot;</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">item</span>, <span class="ruby-identifier">obj</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;#{obj}: #{item}&quot;</span>
<span class="ruby-keyword">end</span>

<span class="ruby-comment"># foo: one</span>
<span class="ruby-comment"># foo: two</span>
<span class="ruby-comment"># foo: three</span>

<span class="ruby-identifier">enum_with_obj</span> = <span class="ruby-identifier">enumerator</span>.<span class="ruby-identifier">each_with_object</span>(<span class="ruby-string">&quot;foo&quot;</span>)
<span class="ruby-identifier">puts</span> <span class="ruby-identifier">enum_with_obj</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># =&gt; Enumerator</span>

<span class="ruby-identifier">enum_with_obj</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">item</span>, <span class="ruby-identifier">obj</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;#{obj}: #{item}&quot;</span>
<span class="ruby-keyword">end</span>

<span class="ruby-comment"># foo: one</span>
<span class="ruby-comment"># foo: two</span>
<span class="ruby-comment"># foo: three</span>
</pre>

<p>This allows you to chain Enumerators together.  For example, you can map a list’s elements to strings containing the index and the element as a string via:</p>

<pre class="ruby"><span class="ruby-identifier">puts</span> <span class="ruby-node">%w[foo bar baz]</span>.<span class="ruby-identifier">map</span>.<span class="ruby-identifier">with_index</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">w</span>, <span class="ruby-identifier">i</span><span class="ruby-operator">|</span> <span class="ruby-node">&quot;#{i}:#{w}&quot;</span> }
<span class="ruby-comment"># =&gt; [&quot;0:foo&quot;, &quot;1:bar&quot;, &quot;2:baz&quot;]</span>
</pre>

<h2 id="class-Enumerator-label-External+Iteration">External Iteration<span><a href="#class-Enumerator-label-External+Iteration">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>An <a href="Enumerator.html"><code>Enumerator</code></a> can also be used as an external iterator. For example, <a href="Enumerator.html#method-i-next"><code>Enumerator#next</code></a> returns the next value of the iterator or raises <a href="StopIteration.html"><code>StopIteration</code></a> if the <a href="Enumerator.html"><code>Enumerator</code></a> is at the end.</p>

<pre class="ruby"><span class="ruby-identifier">e</span> = [<span class="ruby-value">1</span>,<span class="ruby-value">2</span>,<span class="ruby-value">3</span>].<span class="ruby-identifier">each</span>   <span class="ruby-comment"># returns an enumerator object.</span>
<span class="ruby-identifier">puts</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span>   <span class="ruby-comment"># =&gt; 1</span>
<span class="ruby-identifier">puts</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span>   <span class="ruby-comment"># =&gt; 2</span>
<span class="ruby-identifier">puts</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span>   <span class="ruby-comment"># =&gt; 3</span>
<span class="ruby-identifier">puts</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span>   <span class="ruby-comment"># raises StopIteration</span>
</pre>

<p><code>next</code>, <code>next_values</code>, <code>peek</code> and <code>peek_values</code> are the only methods which use external iteration (and <a href="Array.html#method-i-zip"><code>Array#zip(Enumerable-not-Array)</code></a> which uses <code>next</code>).</p>

<p>These methods do not affect other internal enumeration methods, unless the underlying iteration method itself has side-effect, e.g. <a href="IO.html#method-i-each_line"><code>IO#each_line</code></a>.</p>

<p>External iteration differs <strong>significantly</strong> from internal iteration due to using a Fiber:</p>
<ul><li>
<p>The <a href="Fiber.html"><code>Fiber</code></a> adds some overhead compared to internal enumeration.</p>
</li><li>
<p>The stacktrace will only include the stack from the <a href="Enumerator.html"><code>Enumerator</code></a>, not above.</p>
</li><li>
<p>Fiber-local variables are <strong>not</strong> inherited inside the <a href="Enumerator.html"><code>Enumerator</code></a> <a href="Fiber.html"><code>Fiber</code></a>, which instead starts with no Fiber-local variables.</p>
</li><li>
<p><a href="Fiber.html"><code>Fiber</code></a> storage variables <strong>are</strong> inherited and are designed to handle <a href="Enumerator.html"><code>Enumerator</code></a> Fibers. Assigning to a <a href="Fiber.html"><code>Fiber</code></a> storage variable only affects the current <a href="Fiber.html"><code>Fiber</code></a>, so if you want to change state in the caller <a href="Fiber.html"><code>Fiber</code></a> of the <a href="Enumerator.html"><code>Enumerator</code></a> <a href="Fiber.html"><code>Fiber</code></a>, you need to use an extra indirection (e.g., use some object in the <a href="Fiber.html"><code>Fiber</code></a> storage variable and mutate some ivar of it).</p>
</li></ul>

<p>Concretely:</p>

<pre class="ruby"><span class="ruby-constant">Thread</span>.<span class="ruby-identifier">current</span>[<span class="ruby-value">:fiber_local</span>] = <span class="ruby-value">1</span>
<span class="ruby-constant">Fiber</span>[<span class="ruby-value">:storage_var</span>] = <span class="ruby-value">1</span>
<span class="ruby-identifier">e</span> = <span class="ruby-constant">Enumerator</span>.<span class="ruby-identifier">new</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">y</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">p</span> <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">current</span>[<span class="ruby-value">:fiber_local</span>] <span class="ruby-comment"># for external iteration: nil, for internal iteration: 1</span>
  <span class="ruby-identifier">p</span> <span class="ruby-constant">Fiber</span>[<span class="ruby-value">:storage_var</span>] <span class="ruby-comment"># =&gt; 1, inherited</span>
  <span class="ruby-constant">Fiber</span>[<span class="ruby-value">:storage_var</span>] <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>
  <span class="ruby-identifier">y</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-value">42</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span> <span class="ruby-comment"># =&gt; 42</span>
<span class="ruby-identifier">p</span> <span class="ruby-constant">Fiber</span>[<span class="ruby-value">:storage_var</span>] <span class="ruby-comment"># =&gt; 1 (it ran in a different Fiber)</span>

<span class="ruby-identifier">e</span>.<span class="ruby-identifier">each</span> { <span class="ruby-identifier">p</span> <span class="ruby-identifier">_1</span> }
<span class="ruby-identifier">p</span> <span class="ruby-constant">Fiber</span>[<span class="ruby-value">:storage_var</span>] <span class="ruby-comment"># =&gt; 2 (it ran in the same Fiber/&quot;stack&quot; as the current Fiber)</span>
</pre>

<h2 id="class-Enumerator-label-Convert+External+Iteration+to+Internal+Iteration">Convert External Iteration to Internal Iteration<span><a href="#class-Enumerator-label-Convert+External+Iteration+to+Internal+Iteration">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>You can use an external iterator to implement an internal iterator as follows:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">ext_each</span>(<span class="ruby-identifier">e</span>)
  <span class="ruby-keyword">while</span> <span class="ruby-keyword">true</span>
    <span class="ruby-keyword">begin</span>
      <span class="ruby-identifier">vs</span> = <span class="ruby-identifier">e</span>.<span class="ruby-identifier">next_values</span>
    <span class="ruby-keyword">rescue</span> <span class="ruby-constant">StopIteration</span>
      <span class="ruby-keyword">return</span> <span class="ruby-identifier">$!</span>.<span class="ruby-identifier">result</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-identifier">y</span> = <span class="ruby-keyword">yield</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">vs</span>)
    <span class="ruby-identifier">e</span>.<span class="ruby-identifier">feed</span> <span class="ruby-identifier">y</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">o</span> = <span class="ruby-constant">Object</span>.<span class="ruby-identifier">new</span>

<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">o</span>.<span class="ruby-identifier">each</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-keyword">yield</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-keyword">yield</span>(<span class="ruby-value">1</span>)
  <span class="ruby-identifier">puts</span> <span class="ruby-keyword">yield</span>(<span class="ruby-value">1</span>, <span class="ruby-value">2</span>)
  <span class="ruby-value">3</span>
<span class="ruby-keyword">end</span>

<span class="ruby-comment"># use o.each as an internal iterator directly.</span>
<span class="ruby-identifier">puts</span> <span class="ruby-identifier">o</span>.<span class="ruby-identifier">each</span> {<span class="ruby-operator">|</span><span class="ruby-operator">*</span><span class="ruby-identifier">x</span><span class="ruby-operator">|</span> <span class="ruby-identifier">puts</span> <span class="ruby-identifier">x</span>; [<span class="ruby-value">:b</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">x</span>] }
<span class="ruby-comment"># =&gt; [], [:b], [1], [:b, 1], [1, 2], [:b, 1, 2], 3</span>

<span class="ruby-comment"># convert o.each to an external iterator for</span>
<span class="ruby-comment"># implementing an internal iterator.</span>
<span class="ruby-identifier">puts</span> <span class="ruby-identifier">ext_each</span>(<span class="ruby-identifier">o</span>.<span class="ruby-identifier">to_enum</span>) {<span class="ruby-operator">|</span><span class="ruby-operator">*</span><span class="ruby-identifier">x</span><span class="ruby-operator">|</span> <span class="ruby-identifier">puts</span> <span class="ruby-identifier">x</span>; [<span class="ruby-value">:b</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">x</span>] }
<span class="ruby-comment"># =&gt; [], [:b], [1], [:b, 1], [1, 2], [:b, 1, 2], 3</span>
</pre>

    </section>

      <section id="5Buntitled-5D" class="documentation-section">





                <section id="public-class-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Class Methods</h3>
                </header>

                  <div id="method-c-new" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          new(size = nil) { |yielder| ... }
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Creates a new <a href="Enumerator.html"><code>Enumerator</code></a> object, which can be used as an <a href="Enumerable.html"><code>Enumerable</code></a>.</p>

<p>Iteration is defined by the given block, in which a “yielder” object, given as block parameter, can be used to yield a value by calling the <code>yield</code> method (aliased as <code>&lt;&lt;</code>):</p>

<pre class="ruby"><span class="ruby-identifier">fib</span> = <span class="ruby-constant">Enumerator</span>.<span class="ruby-identifier">new</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">y</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">a</span> = <span class="ruby-identifier">b</span> = <span class="ruby-value">1</span>
  <span class="ruby-identifier">loop</span> <span class="ruby-keyword">do</span>
    <span class="ruby-identifier">y</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">a</span>
    <span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span> = <span class="ruby-identifier">b</span>, <span class="ruby-identifier">a</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">b</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">fib</span>.<span class="ruby-identifier">take</span>(<span class="ruby-value">10</span>) <span class="ruby-comment"># =&gt; [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]</span>
</pre>

<p>The optional parameter can be used to specify how to calculate the size in a lazy fashion (see <a href="Enumerator.html#method-i-size"><code>Enumerator#size</code></a>). It can either be a value or a callable object.</p>

                              <div class="method-source-code" id="new-source">
            <pre>static VALUE
enumerator_initialize(int argc, VALUE *argv, VALUE obj)
{
    VALUE iter = rb_block_proc();
    VALUE recv = generator_init(generator_allocate(rb_cGenerator), iter);
    VALUE arg0 = rb_check_arity(argc, 0, 1) ? argv[0] : Qnil;
    VALUE size = convert_to_feasible_size_value(arg0);

    return enumerator_init(obj, recv, sym_each, 0, 0, 0, size, false);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-produce" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          produce(initial = nil) { |prev| block } &rarr; enumerator
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Creates an infinite enumerator from any block, just called over and over.  The result of the previous iteration is passed to the next one. If <code>initial</code> is provided, it is passed to the first iteration, and becomes the first element of the enumerator; if it is not provided, the first iteration receives <code>nil</code>, and its result becomes the first element of the iterator.</p>

<p>Raising <a href="StopIteration.html"><code>StopIteration</code></a> from the block stops an iteration.</p>

<pre class="ruby"><span class="ruby-constant">Enumerator</span>.<span class="ruby-identifier">produce</span>(<span class="ruby-value">1</span>, <span class="ruby-operator">&amp;</span><span class="ruby-value">:succ</span>)   <span class="ruby-comment"># =&gt; enumerator of 1, 2, 3, 4, ....</span>

<span class="ruby-constant">Enumerator</span>.<span class="ruby-identifier">produce</span> { <span class="ruby-identifier">rand</span>(<span class="ruby-value">10</span>) } <span class="ruby-comment"># =&gt; infinite random number sequence</span>

<span class="ruby-identifier">ancestors</span> = <span class="ruby-constant">Enumerator</span>.<span class="ruby-identifier">produce</span>(<span class="ruby-identifier">node</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">prev</span><span class="ruby-operator">|</span> <span class="ruby-identifier">node</span> = <span class="ruby-identifier">prev</span>.<span class="ruby-identifier">parent</span> <span class="ruby-keyword">or</span> <span class="ruby-identifier">raise</span> <span class="ruby-constant">StopIteration</span> }
<span class="ruby-identifier">enclosing_section</span> = <span class="ruby-identifier">ancestors</span>.<span class="ruby-identifier">find</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">n</span><span class="ruby-operator">|</span> <span class="ruby-identifier">n</span>.<span class="ruby-identifier">type</span> <span class="ruby-operator">==</span> <span class="ruby-value">:section</span> }
</pre>

<p>Using <a href="Enumerator.html#method-c-produce"><code>::produce</code></a> together with <a href="Enumerable.html"><code>Enumerable</code></a> methods like <a href="Enumerable.html#method-i-detect"><code>Enumerable#detect</code></a>, <a href="Enumerable.html#method-i-slice_after"><code>Enumerable#slice_after</code></a>, <a href="Enumerable.html#method-i-take_while"><code>Enumerable#take_while</code></a> can provide Enumerator-based alternatives for <code>while</code> and <code>until</code> cycles:</p>

<pre class="ruby"><span class="ruby-comment"># Find next Tuesday</span>
<span class="ruby-identifier">require</span> <span class="ruby-string">&quot;date&quot;</span>
<span class="ruby-constant">Enumerator</span>.<span class="ruby-identifier">produce</span>(<span class="ruby-constant">Date</span>.<span class="ruby-identifier">today</span>, <span class="ruby-operator">&amp;</span><span class="ruby-value">:succ</span>).<span class="ruby-identifier">detect</span>(<span class="ruby-operator">&amp;</span><span class="ruby-value">:tuesday?</span>)

<span class="ruby-comment"># Simple lexer:</span>
<span class="ruby-identifier">require</span> <span class="ruby-string">&quot;strscan&quot;</span>
<span class="ruby-identifier">scanner</span> = <span class="ruby-constant">StringScanner</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&quot;7+38/6&quot;</span>)
<span class="ruby-constant">PATTERN</span> = <span class="ruby-regexp">%r{\d+|[-/+*]}</span>
<span class="ruby-constant">Enumerator</span>.<span class="ruby-identifier">produce</span> { <span class="ruby-identifier">scanner</span>.<span class="ruby-identifier">scan</span>(<span class="ruby-constant">PATTERN</span>) }.<span class="ruby-identifier">slice_after</span> { <span class="ruby-identifier">scanner</span>.<span class="ruby-identifier">eos?</span> }.<span class="ruby-identifier">first</span>
<span class="ruby-comment"># =&gt; [&quot;7&quot;, &quot;+&quot;, &quot;38&quot;, &quot;/&quot;, &quot;6&quot;]</span>
</pre>

                              <div class="method-source-code" id="produce-source">
            <pre>static VALUE
enumerator_s_produce(int argc, VALUE *argv, VALUE klass)
{
    VALUE init, producer;

    if (!rb_block_given_p()) rb_raise(rb_eArgError, &quot;no block given&quot;);

    if (rb_scan_args(argc, argv, &quot;01&quot;, &amp;init) == 0) {
        init = Qundef;
    }

    producer = producer_init(producer_allocate(rb_cEnumProducer), init, rb_block_proc());

    return rb_enumeratorize_with_size_kw(producer, sym_each, 0, 0, producer_size, RB_NO_KEYWORDS);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-product" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          product(*enums) &rarr; enumerator
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          product(*enums) { |elts| ... } &rarr; enumerator
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Generates a new enumerator object that generates a Cartesian product of given enumerable objects.  This is equivalent to <a href="Enumerator/Product.html#method-c-new"><code>Enumerator::Product.new</code></a>.</p>

<pre class="ruby"><span class="ruby-identifier">e</span> = <span class="ruby-constant">Enumerator</span>.<span class="ruby-identifier">product</span>(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">3</span>, [<span class="ruby-value">4</span>, <span class="ruby-value">5</span>])
<span class="ruby-identifier">e</span>.<span class="ruby-identifier">to_a</span> <span class="ruby-comment">#=&gt; [[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]]</span>
<span class="ruby-identifier">e</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment">#=&gt; 6</span>
</pre>

<p>When a block is given, calls the block with each N-element array generated and returns <code>nil</code>.</p>

                              <div class="method-source-code" id="product-source">
            <pre>static VALUE
enumerator_s_product(int argc, VALUE *argv, VALUE klass)
{
    VALUE enums = Qnil, options = Qnil, block = Qnil;

    rb_scan_args(argc, argv, &quot;*:&amp;&quot;, &amp;enums, &amp;options, &amp;block);

    if (!NIL_P(options) &amp;&amp; !RHASH_EMPTY_P(options)) {
        rb_exc_raise(rb_keyword_error_new(&quot;unknown&quot;, rb_hash_keys(options)));
    }

    VALUE obj = enum_product_initialize(argc, argv, enum_product_allocate(rb_cEnumProduct));

    if (!NIL_P(block)) {
        enum_product_run(obj, block);
        return Qnil;
    }

    return obj;
}</pre>
                              </div>
                            </div>


                          </div>

                          </section>

                <section id="public-instance-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Instance Methods</h3>
                </header>

                  <div id="method-i-2B" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          e + enum &rarr; enumerator
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns an enumerator object generated from this enumerator and a given enumerable.</p>

<pre class="ruby"><span class="ruby-identifier">e</span> = (<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">3</span>).<span class="ruby-identifier">each</span> <span class="ruby-operator">+</span> [<span class="ruby-value">4</span>, <span class="ruby-value">5</span>]
<span class="ruby-identifier">e</span>.<span class="ruby-identifier">to_a</span> <span class="ruby-comment">#=&gt; [1, 2, 3, 4, 5]</span>
</pre>

                              <div class="method-source-code" id="2B-source">
            <pre>static VALUE
enumerator_plus(VALUE obj, VALUE eobj)
{
    return new_enum_chain(rb_ary_new_from_args(2, obj, eobj));
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-each" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          each { |elm| block }                    &rarr; obj
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          each                                    &rarr; enum
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          each(*appending_args) { |elm| block }   &rarr; obj
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          each(*appending_args)                   &rarr; an_enumerator
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Iterates over the block according to how this <a href="Enumerator.html"><code>Enumerator</code></a> was constructed. If no block and no arguments are given, returns self.</p>

<h3 id="method-i-each-label-Examples">Examples<span><a href="#method-i-each-label-Examples">&para;</a> <a href="#top">&uarr;</a></span></h3>

<pre class="ruby"><span class="ruby-string">&quot;Hello, world!&quot;</span>.<span class="ruby-identifier">scan</span>(<span class="ruby-regexp">/\w+/</span>)                     <span class="ruby-comment">#=&gt; [&quot;Hello&quot;, &quot;world&quot;]</span>
<span class="ruby-string">&quot;Hello, world!&quot;</span>.<span class="ruby-identifier">to_enum</span>(<span class="ruby-value">:scan</span>, <span class="ruby-regexp">/\w+/</span>).<span class="ruby-identifier">to_a</span>      <span class="ruby-comment">#=&gt; [&quot;Hello&quot;, &quot;world&quot;]</span>
<span class="ruby-string">&quot;Hello, world!&quot;</span>.<span class="ruby-identifier">to_enum</span>(<span class="ruby-value">:scan</span>).<span class="ruby-identifier">each</span>(<span class="ruby-regexp">/\w+/</span>).<span class="ruby-identifier">to_a</span> <span class="ruby-comment">#=&gt; [&quot;Hello&quot;, &quot;world&quot;]</span>

<span class="ruby-identifier">obj</span> = <span class="ruby-constant">Object</span>.<span class="ruby-identifier">new</span>

<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">obj</span>.<span class="ruby-identifier">each_arg</span>(<span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span>=<span class="ruby-value">:b</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">rest</span>)
  <span class="ruby-keyword">yield</span> <span class="ruby-identifier">a</span>
  <span class="ruby-keyword">yield</span> <span class="ruby-identifier">b</span>
  <span class="ruby-keyword">yield</span> <span class="ruby-identifier">rest</span>
  <span class="ruby-value">:method_returned</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">enum</span> = <span class="ruby-identifier">obj</span>.<span class="ruby-identifier">to_enum</span> <span class="ruby-value">:each_arg</span>, <span class="ruby-value">:a</span>, <span class="ruby-value">:x</span>

<span class="ruby-identifier">enum</span>.<span class="ruby-identifier">each</span>.<span class="ruby-identifier">to_a</span>                  <span class="ruby-comment">#=&gt; [:a, :x, []]</span>
<span class="ruby-identifier">enum</span>.<span class="ruby-identifier">each</span>.<span class="ruby-identifier">equal?</span>(<span class="ruby-identifier">enum</span>)          <span class="ruby-comment">#=&gt; true</span>
<span class="ruby-identifier">enum</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">elm</span><span class="ruby-operator">|</span> <span class="ruby-identifier">elm</span> }         <span class="ruby-comment">#=&gt; :method_returned</span>

<span class="ruby-identifier">enum</span>.<span class="ruby-identifier">each</span>(<span class="ruby-value">:y</span>, <span class="ruby-value">:z</span>).<span class="ruby-identifier">to_a</span>          <span class="ruby-comment">#=&gt; [:a, :x, [:y, :z]]</span>
<span class="ruby-identifier">enum</span>.<span class="ruby-identifier">each</span>(<span class="ruby-value">:y</span>, <span class="ruby-value">:z</span>).<span class="ruby-identifier">equal?</span>(<span class="ruby-identifier">enum</span>)  <span class="ruby-comment">#=&gt; false</span>
<span class="ruby-identifier">enum</span>.<span class="ruby-identifier">each</span>(<span class="ruby-value">:y</span>, <span class="ruby-value">:z</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">elm</span><span class="ruby-operator">|</span> <span class="ruby-identifier">elm</span> } <span class="ruby-comment">#=&gt; :method_returned</span>
</pre>

                              <div class="method-source-code" id="each-source">
            <pre>static VALUE
enumerator_each(int argc, VALUE *argv, VALUE obj)
{
    struct enumerator *e = enumerator_ptr(obj);

    if (argc &gt; 0) {
        VALUE args = (e = enumerator_ptr(obj = rb_obj_dup(obj)))-&gt;args;
        if (args) {
#if SIZEOF_INT &lt; SIZEOF_LONG
            /* check int range overflow */
            rb_long2int(RARRAY_LEN(args) + argc);
#endif
            args = rb_ary_dup(args);
            rb_ary_cat(args, argv, argc);
        }
        else {
            args = rb_ary_new4(argc, argv);
        }
        e-&gt;args = args;
        e-&gt;size = Qnil;
        e-&gt;size_fn = 0;
    }
    if (!rb_block_given_p()) return obj;

    if (!lazy_precheck(e-&gt;procs)) return Qnil;

    return enumerator_block_call(obj, 0, obj);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-each_with_index" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          each_with_index {|(*args), idx| ... }
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          each_with_index
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Same as <a href="Enumerator.html#method-i-with_index"><code>Enumerator#with_index(0)</code></a>, i.e. there is no starting offset.</p>

<p>If no block is given, a new <a href="Enumerator.html"><code>Enumerator</code></a> is returned that includes the index.</p>

                              <div class="method-source-code" id="each_with_index-source">
            <pre>static VALUE
enumerator_each_with_index(VALUE obj)
{
    return enumerator_with_index(0, NULL, obj);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-each_with_object" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          each_with_object(obj) {|(*args), obj| ... }
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          each_with_object(obj)
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Iterates the given block for each element with an arbitrary object, <code>obj</code>, and returns <code>obj</code></p>

<p>If no block is given, returns a new <a href="Enumerator.html"><code>Enumerator</code></a>.</p>

<h3 id="method-i-each_with_object-label-Example">Example<span><a href="#method-i-each_with_object-label-Example">&para;</a> <a href="#top">&uarr;</a></span></h3>

<pre class="ruby"><span class="ruby-identifier">to_three</span> = <span class="ruby-constant">Enumerator</span>.<span class="ruby-identifier">new</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">y</span><span class="ruby-operator">|</span>
  <span class="ruby-value">3</span>.<span class="ruby-identifier">times</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">x</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">y</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">x</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">to_three_with_string</span> = <span class="ruby-identifier">to_three</span>.<span class="ruby-identifier">with_object</span>(<span class="ruby-string">&quot;foo&quot;</span>)
<span class="ruby-identifier">to_three_with_string</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">x</span>,<span class="ruby-identifier">string</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;#{string}: #{x}&quot;</span>
<span class="ruby-keyword">end</span>

<span class="ruby-comment"># =&gt; foo: 0</span>
<span class="ruby-comment"># =&gt; foo: 1</span>
<span class="ruby-comment"># =&gt; foo: 2</span>
</pre>

                              <div class="method-source-code" id="each_with_object-source">
            <pre>static VALUE
enumerator_with_object(VALUE obj, VALUE memo)
{
    RETURN_SIZED_ENUMERATOR(obj, 1, &amp;memo, enumerator_enum_size);
    enumerator_block_call(obj, enumerator_with_object_i, memo);

    return memo;
}</pre>
                              </div>
                            </div>

                            <div class="aliases">
                              Also aliased as: <a href="Enumerator.html#method-i-with_object">with_object</a>
                            </div>

                          </div>

                  <div id="method-i-feed" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          feed obj   &rarr; nil
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Sets the value to be returned by the next yield inside <code>e</code>.</p>

<p>If the value is not set, the yield returns nil.</p>

<p>This value is cleared after being yielded.</p>

<pre class="ruby"><span class="ruby-comment"># Array#map passes the array&#39;s elements to &quot;yield&quot; and collects the</span>
<span class="ruby-comment"># results of &quot;yield&quot; as an array.</span>
<span class="ruby-comment"># Following example shows that &quot;next&quot; returns the passed elements and</span>
<span class="ruby-comment"># values passed to &quot;feed&quot; are collected as an array which can be</span>
<span class="ruby-comment"># obtained by StopIteration#result.</span>
<span class="ruby-identifier">e</span> = [<span class="ruby-value">1</span>,<span class="ruby-value">2</span>,<span class="ruby-value">3</span>].<span class="ruby-identifier">map</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span>           <span class="ruby-comment">#=&gt; 1</span>
<span class="ruby-identifier">e</span>.<span class="ruby-identifier">feed</span> <span class="ruby-string">&quot;a&quot;</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span>           <span class="ruby-comment">#=&gt; 2</span>
<span class="ruby-identifier">e</span>.<span class="ruby-identifier">feed</span> <span class="ruby-string">&quot;b&quot;</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span>           <span class="ruby-comment">#=&gt; 3</span>
<span class="ruby-identifier">e</span>.<span class="ruby-identifier">feed</span> <span class="ruby-string">&quot;c&quot;</span>
<span class="ruby-keyword">begin</span>
  <span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span>
<span class="ruby-keyword">rescue</span> <span class="ruby-constant">StopIteration</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">$!</span>.<span class="ruby-identifier">result</span>      <span class="ruby-comment">#=&gt; [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">o</span> = <span class="ruby-constant">Object</span>.<span class="ruby-identifier">new</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">o</span>.<span class="ruby-identifier">each</span>
  <span class="ruby-identifier">x</span> = <span class="ruby-keyword">yield</span>         <span class="ruby-comment"># (2) blocks</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">x</span>               <span class="ruby-comment"># (5) =&gt; &quot;foo&quot;</span>
  <span class="ruby-identifier">x</span> = <span class="ruby-keyword">yield</span>         <span class="ruby-comment"># (6) blocks</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">x</span>               <span class="ruby-comment"># (8) =&gt; nil</span>
  <span class="ruby-identifier">x</span> = <span class="ruby-keyword">yield</span>         <span class="ruby-comment"># (9) blocks</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">x</span>               <span class="ruby-comment"># not reached w/o another e.next</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">e</span> = <span class="ruby-identifier">o</span>.<span class="ruby-identifier">to_enum</span>
<span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span>              <span class="ruby-comment"># (1)</span>
<span class="ruby-identifier">e</span>.<span class="ruby-identifier">feed</span> <span class="ruby-string">&quot;foo&quot;</span>        <span class="ruby-comment"># (3)</span>
<span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span>              <span class="ruby-comment"># (4)</span>
<span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span>              <span class="ruby-comment"># (7)</span>
                    <span class="ruby-comment"># (10)</span>
</pre>

                              <div class="method-source-code" id="feed-source">
            <pre>static VALUE
enumerator_feed(VALUE obj, VALUE v)
{
    struct enumerator *e = enumerator_ptr(obj);

    if (!UNDEF_P(e-&gt;feedvalue)) {
        rb_raise(rb_eTypeError, &quot;feed value already set&quot;);
    }
    e-&gt;feedvalue = v;

    return Qnil;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-inspect" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          inspect  &rarr; string
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Creates a printable version of <em>e</em>.</p>

                              <div class="method-source-code" id="inspect-source">
            <pre>static VALUE
enumerator_inspect(VALUE obj)
{
    return rb_exec_recursive(inspect_enumerator, obj, 0);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-next" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          next   &rarr; object
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the next object in the enumerator, and move the internal position forward.  When the position reached at the end, <a href="StopIteration.html"><code>StopIteration</code></a> is raised.</p>

<h3 id="method-i-next-label-Example">Example<span><a href="#method-i-next-label-Example">&para;</a> <a href="#top">&uarr;</a></span></h3>

<pre class="ruby"><span class="ruby-identifier">a</span> = [<span class="ruby-value">1</span>,<span class="ruby-value">2</span>,<span class="ruby-value">3</span>]
<span class="ruby-identifier">e</span> = <span class="ruby-identifier">a</span>.<span class="ruby-identifier">to_enum</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span>   <span class="ruby-comment">#=&gt; 1</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span>   <span class="ruby-comment">#=&gt; 2</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span>   <span class="ruby-comment">#=&gt; 3</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span>   <span class="ruby-comment">#raises StopIteration</span>
</pre>

<p>See class-level notes about external iterators.</p>

                              <div class="method-source-code" id="next-source">
            <pre>static VALUE
enumerator_next(VALUE obj)
{
    VALUE vs = enumerator_next_values(obj);
    return ary2sv(vs, 0);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-next_values" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          next_values   &rarr; array
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the next object as an array in the enumerator, and move the internal position forward.  When the position reached at the end, <a href="StopIteration.html"><code>StopIteration</code></a> is raised.</p>

<p>See class-level notes about external iterators.</p>

<p>This method can be used to distinguish <code>yield</code> and <code>yield nil</code>.</p>

<h3 id="method-i-next_values-label-Example">Example<span><a href="#method-i-next_values-label-Example">&para;</a> <a href="#top">&uarr;</a></span></h3>

<pre class="ruby"><span class="ruby-identifier">o</span> = <span class="ruby-constant">Object</span>.<span class="ruby-identifier">new</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">o</span>.<span class="ruby-identifier">each</span>
  <span class="ruby-keyword">yield</span>
  <span class="ruby-keyword">yield</span> <span class="ruby-value">1</span>
  <span class="ruby-keyword">yield</span> <span class="ruby-value">1</span>, <span class="ruby-value">2</span>
  <span class="ruby-keyword">yield</span> <span class="ruby-keyword">nil</span>
  <span class="ruby-keyword">yield</span> [<span class="ruby-value">1</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">e</span> = <span class="ruby-identifier">o</span>.<span class="ruby-identifier">to_enum</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">next_values</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">next_values</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">next_values</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">next_values</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">next_values</span>
<span class="ruby-identifier">e</span> = <span class="ruby-identifier">o</span>.<span class="ruby-identifier">to_enum</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span>

<span class="ruby-comment">## yield args       next_values      next</span>
<span class="ruby-comment">#  yield            []               nil</span>
<span class="ruby-comment">#  yield 1          [1]              1</span>
<span class="ruby-comment">#  yield 1, 2       [1, 2]           [1, 2]</span>
<span class="ruby-comment">#  yield nil        [nil]            nil</span>
<span class="ruby-comment">#  yield [1, 2]     [[1, 2]]         [1, 2]</span>
</pre>

                              <div class="method-source-code" id="next_values-source">
            <pre>static VALUE
enumerator_next_values(VALUE obj)
{
    struct enumerator *e = enumerator_ptr(obj);
    VALUE vs;

    if (!UNDEF_P(e-&gt;lookahead)) {
        vs = e-&gt;lookahead;
        e-&gt;lookahead = Qundef;
        return vs;
    }

    return get_next_values(obj, e);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-peek" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          peek   &rarr; object
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the next object in the enumerator, but doesn’t move the internal position forward.  If the position is already at the end, <a href="StopIteration.html"><code>StopIteration</code></a> is raised.</p>

<p>See class-level notes about external iterators.</p>

<h3 id="method-i-peek-label-Example">Example<span><a href="#method-i-peek-label-Example">&para;</a> <a href="#top">&uarr;</a></span></h3>

<pre class="ruby"><span class="ruby-identifier">a</span> = [<span class="ruby-value">1</span>,<span class="ruby-value">2</span>,<span class="ruby-value">3</span>]
<span class="ruby-identifier">e</span> = <span class="ruby-identifier">a</span>.<span class="ruby-identifier">to_enum</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span>   <span class="ruby-comment">#=&gt; 1</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">peek</span>   <span class="ruby-comment">#=&gt; 2</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">peek</span>   <span class="ruby-comment">#=&gt; 2</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">peek</span>   <span class="ruby-comment">#=&gt; 2</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span>   <span class="ruby-comment">#=&gt; 2</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span>   <span class="ruby-comment">#=&gt; 3</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">peek</span>   <span class="ruby-comment">#raises StopIteration</span>
</pre>

                              <div class="method-source-code" id="peek-source">
            <pre>static VALUE
enumerator_peek(VALUE obj)
{
    VALUE vs = enumerator_peek_values(obj);
    return ary2sv(vs, 1);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-peek_values" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          peek_values   &rarr; array
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the next object as an array, similar to <a href="Enumerator.html#method-i-next_values"><code>Enumerator#next_values</code></a>, but doesn’t move the internal position forward.  If the position is already at the end, <a href="StopIteration.html"><code>StopIteration</code></a> is raised.</p>

<p>See class-level notes about external iterators.</p>

<h3 id="method-i-peek_values-label-Example">Example<span><a href="#method-i-peek_values-label-Example">&para;</a> <a href="#top">&uarr;</a></span></h3>

<pre class="ruby"><span class="ruby-identifier">o</span> = <span class="ruby-constant">Object</span>.<span class="ruby-identifier">new</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">o</span>.<span class="ruby-identifier">each</span>
  <span class="ruby-keyword">yield</span>
  <span class="ruby-keyword">yield</span> <span class="ruby-value">1</span>
  <span class="ruby-keyword">yield</span> <span class="ruby-value">1</span>, <span class="ruby-value">2</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">e</span> = <span class="ruby-identifier">o</span>.<span class="ruby-identifier">to_enum</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">peek_values</span>    <span class="ruby-comment">#=&gt; []</span>
<span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">peek_values</span>    <span class="ruby-comment">#=&gt; [1]</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">peek_values</span>    <span class="ruby-comment">#=&gt; [1]</span>
<span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">peek_values</span>    <span class="ruby-comment">#=&gt; [1, 2]</span>
<span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">peek_values</span>    <span class="ruby-comment"># raises StopIteration</span>
</pre>

                              <div class="method-source-code" id="peek_values-source">
            <pre>static VALUE
enumerator_peek_values_m(VALUE obj)
{
    return rb_ary_dup(enumerator_peek_values(obj));
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-rewind" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          rewind   &rarr; e
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Rewinds the enumeration sequence to the beginning.</p>

<p>If the enclosed object responds to a “rewind” method, it is called.</p>

                              <div class="method-source-code" id="rewind-source">
            <pre>static VALUE
enumerator_rewind(VALUE obj)
{
    struct enumerator *e = enumerator_ptr(obj);

    rb_check_funcall(e-&gt;obj, id_rewind, 0, 0);

    e-&gt;fib = 0;
    e-&gt;dst = Qnil;
    e-&gt;lookahead = Qundef;
    e-&gt;feedvalue = Qundef;
    e-&gt;stop_exc = Qfalse;
    return obj;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-size" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          size          &rarr; int, Float::INFINITY or nil
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the size of the enumerator, or <code>nil</code> if it can’t be calculated lazily.</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">100</span>).<span class="ruby-identifier">to_a</span>.<span class="ruby-identifier">permutation</span>(<span class="ruby-value">4</span>).<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 94109400</span>
<span class="ruby-identifier">loop</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; Float::INFINITY</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">100</span>).<span class="ruby-identifier">drop_while</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; nil</span>
</pre>

                              <div class="method-source-code" id="size-source">
            <pre>static VALUE
enumerator_size(VALUE obj)
{
    struct enumerator *e = enumerator_ptr(obj);
    int argc = 0;
    const VALUE *argv = NULL;
    VALUE size;

    if (e-&gt;procs) {
        struct generator *g = generator_ptr(e-&gt;obj);
        VALUE receiver = rb_check_funcall(g-&gt;obj, id_size, 0, 0);
        long i = 0;

        for (i = 0; i &lt; RARRAY_LEN(e-&gt;procs); i++) {
            VALUE proc = RARRAY_AREF(e-&gt;procs, i);
            struct proc_entry *entry = proc_entry_ptr(proc);
            lazyenum_size_func *size_fn = entry-&gt;fn-&gt;size;
            if (!size_fn) {
                return Qnil;
            }
            receiver = (*size_fn)(proc, receiver);
        }
        return receiver;
    }

    if (e-&gt;size_fn) {
        return (*e-&gt;size_fn)(e-&gt;obj, e-&gt;args, obj);
    }
    if (e-&gt;args) {
        argc = (int)RARRAY_LEN(e-&gt;args);
        argv = RARRAY_CONST_PTR(e-&gt;args);
    }
    size = rb_check_funcall_kw(e-&gt;size, id_call, argc, argv, e-&gt;kw_splat);
    if (!UNDEF_P(size)) return size;
    return e-&gt;size;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-with_index" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          with_index(offset = 0) {|(*args), idx| ... }
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          with_index(offset = 0)
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Iterates the given block for each element with an index, which starts from <code>offset</code>.  If no block is given, returns a new <a href="Enumerator.html"><code>Enumerator</code></a> that includes the index, starting from <code>offset</code></p>
<dl class="rdoc-list note-list"><dt><code>offset</code>
<dd>
<p>the starting index to use</p>
</dd></dl>

                              <div class="method-source-code" id="with_index-source">
            <pre>static VALUE
enumerator_with_index(int argc, VALUE *argv, VALUE obj)
{
    VALUE memo;

    rb_check_arity(argc, 0, 1);
    RETURN_SIZED_ENUMERATOR(obj, argc, argv, enumerator_enum_size);
    memo = (!argc || NIL_P(memo = argv[0])) ? INT2FIX(0) : rb_to_int(memo);
    return enumerator_block_call(obj, enumerator_with_index_i, (VALUE)MEMO_NEW(memo, 0, 0));
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-with_object" class="method-detail method-alias">
                      <div class="method-heading">
                        <span class="method-callseq">
                          with_object(obj) {|(*args), obj| ... }
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          with_object(obj)
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Iterates the given block for each element with an arbitrary object, <code>obj</code>, and returns <code>obj</code></p>

<p>If no block is given, returns a new <a href="Enumerator.html"><code>Enumerator</code></a>.</p>

<h3 id="method-i-with_object-label-Example">Example<span><a href="#method-i-with_object-label-Example">&para;</a> <a href="#top">&uarr;</a></span></h3>

<pre class="ruby"><span class="ruby-identifier">to_three</span> = <span class="ruby-constant">Enumerator</span>.<span class="ruby-identifier">new</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">y</span><span class="ruby-operator">|</span>
  <span class="ruby-value">3</span>.<span class="ruby-identifier">times</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">x</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">y</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">x</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">to_three_with_string</span> = <span class="ruby-identifier">to_three</span>.<span class="ruby-identifier">with_object</span>(<span class="ruby-string">&quot;foo&quot;</span>)
<span class="ruby-identifier">to_three_with_string</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">x</span>,<span class="ruby-identifier">string</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;#{string}: #{x}&quot;</span>
<span class="ruby-keyword">end</span>

<span class="ruby-comment"># =&gt; foo: 0</span>
<span class="ruby-comment"># =&gt; foo: 1</span>
<span class="ruby-comment"># =&gt; foo: 2</span>
</pre>

                            </div>


                            <div class="aliases">
                              Alias for: <a href="Enumerator.html#method-i-each_with_object">each_with_object</a>
                            </div>
                          </div>

                          </section>

              </section>
              </main>



            </div>  <!--  class='wrapper hdiv' -->


<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a></p>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.4.0.</p>
<p>Based on <a href="https://github.com/ged/darkfish/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.</p>

  
    <p><p><a href="https://ruby-doc.org">Ruby-doc.org</a> is provided by <a href="https://jamesbritt.com">James Britt</a> and <a href="https://neurogami.com">Neurogami</a>. </p><p><a href="https://jamesbritt.bandcamp.com/">Hack your world.  Feed your head.  Live curious</a>.</p>
</p>
  
  </footer>

<script type="text/javascript">


  let ads  = $("#carbonads-container").children().detach();


  function swapMode() {
    var cookieName = 'darkmode';
    var cssDarkmode = Cookies.get(cookieName);
    console.log("***** swapMode! " + cssDarkmode + " *****");


    if (cssDarkmode == "true") {
      console.log("We have dark mode, set the css to light ...");
      $('#rdoccss').attr("href", defaultModeCssHref);
      $('#cssSelect').text("Dark mode");
      cssDarkmode = "false";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    } else {
      console.log("We not have dark mode, set the css to dark ...");
      $('#rdoccss').attr("href", darkModeCsseHref);
      $('#cssSelect').text("Light mode");
      cssDarkmode = "true";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    }

    console.log("  --------------- ");
  }


const vueCssApp = new Vue({
el: '#menubar',
data: {
isDark: false
},
methods: {
toggleClass: function(event){
this.isDark = !this.isDark;
}
}
})

const vueApp = new Vue({
el: '#vapp',
data: { 
isOpen: true
},

mounted() {
this.handleResize();
this.manage_mob_classes();
window.addEventListener('resize', this.handleResize)
//this.isOpen !=  (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},
destroyed() {
window.removeEventListener('resize', this.handleResize)
},
created() {
//manage_mob_classes();
},

methods : {
isMobile() {
  return (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},

  handleResize() {
    if (!this.isMobile()) {
      this.isOpen = window.innerWidth > 800;
    }
  },

  manage_mob_classes() {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
      $("nav").addClass("mob_nav");
      $("main").addClass("mob_main");
      $("#extraz").addClass("mob_extraz");
      $("#carbonads-container").addClass("mob_carbonads-container");
      this.isOpen  = false;
    } else {
      $("nav").removeClass("mob_nav") 
        $("main").removeClass("mob_main");
      $("#extraz").removeClass("mob_extraz");
      $("#carbonads-container").removeClass("mob_carbonads-container");
      this.isOpen  = true;
    }
  },

  toggleNav() {
    this.isOpen =! this.isOpen ;
    // alert("Toggle nav!");
    console.log("toggleNav() click: " + this.isOpen );
  }
}
})

$("#carbonads-container").append(ads);


$(function() {

    var darkmode = Cookies.get("darkmode");
    console.log("Document ready: " + darkmode);

    if ( darkmode  == "true" ) {
      $('#cssSelect').text("Light mode");
    } else {
      $('#cssSelect').text("Dark mode");
     }

    $('body').css('display','block');
    });

</script>

    
  </body> 
</html>

